package com.cndw.kungfu.model.siege.domain;

import java.util.concurrent.ScheduledFuture;

import com.cndw.kungfu.config.ConstantsCost;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.kungfu.vo.siege.SiegePlayerVO;

public class SiegePlayer implements Comparable<SiegePlayer> {

	private long playerId;
	private int playerLevel;
	private String playerName;
	private int reviveCount; // 复活次数
	private boolean death; // 死亡状态
	private boolean attack; // 是否攻击方
	private int reviveTime;// 复活时间
	private boolean active; // 是否在防守列表
	/**
	 * 鼓舞增加的buff
	 */
	private int buffAdd;
	/**
	 * 鼓舞次数
	 */
	private int buffTimes;
	/**
	 * 疲劳次数
	 */
	private int fatigue;
	private ScheduledFuture<?> future;

	public SiegePlayer(long playerId, int playerLevel, String playerName, boolean attack) {
		this.playerId = playerId;
		this.playerLevel = playerLevel;
		this.playerName = playerName;
		this.attack = attack;
		this.active = !attack;
	}

	public long getPlayerId() {
		return playerId;
	}

	public void setPlayerId(long playerId) {
		this.playerId = playerId;
	}

	public int getPlayerLevel() {
		return playerLevel;
	}

	public void setPlayerLevel(int playerLevel) {
		this.playerLevel = playerLevel;
	}

	public String getPlayerName() {
		return playerName;
	}

	public void setPlayerName(String playerName) {
		this.playerName = playerName;
	}

	public int getReviveCount() {
		return reviveCount;
	}

	public void setReviveCount(int reviveCount) {
		this.reviveCount = reviveCount;
	}

	public boolean isDeath() {
		return death;
	}

	public void setDeath(boolean death) {
		this.death = death;
	}

	public boolean isAttack() {
		return attack;
	}

	public void setAttack(boolean attack) {
		this.attack = attack;
	}

	public int getReviveTime() {
		return reviveTime;
	}

	public void setReviveTime(int reviveTime) {
		this.reviveTime = reviveTime;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
	
	

	public int getBuffAdd() {
		return buffAdd;
	}

	public void setBuffAdd(int buffAdd) {
		this.buffAdd = buffAdd;
	}

	public int getBuffTimes() {
		return buffTimes;
	}

	public void setBuffTimes(int buffTimes) {
		this.buffTimes = buffTimes;
	}

	public int getFatigue() {
		return fatigue;
	}

	public void setFatigue(int fatigue) {
		this.fatigue = fatigue;
	}
	
	public void alterFatigue(int alter){
		fatigue = NumberProcesser.between(fatigue+alter, 0, 70);
	}

	public ScheduledFuture<?> getFuture() {
		return future;
	}

	public void setFuture(ScheduledFuture<?> future) {
		this.future = future;
	}

	/**
	 * 被杀死
	 */
	public void killDeath(int time) {
		setDeath(true);
		setActive(false);
		int revive = time + ConstantsModel.siegeReviveTime;
		setReviveTime(TimeProcesser.getUnixTime() + revive);
		future = Context.getTimerServer().scheduleSeconds(new Runnable() {
			@Override
			public void run() {
				setDeath(false);
				Player player = Context.getPlayerServer().getByPlayerId(playerId);
				player.setHp(player.getMaxHp());
				player.setMp(player.getMaxMp());
				if (player.getDataInfo().getPet() != null) {
					player.getDataInfo().getPet().fullHpMp();
				}
			}
		}, revive);
	}

	/**
	 * 立即复活
	 */
	public boolean revive() {
		int gold = 0;
		if (reviveCount > 2) {
			gold = 20;
		} else {
			gold = ConstantsModel.siegeReviveGold[reviveCount];
		}

		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		if (player.getGoldAll() < gold) {
			return false;
		} else {
			reviveCount++;
			player.alterGoldAll(-gold);
			player.setHp(player.getMaxHp());
			player.setMp(player.getMaxMp());	
			player.commitSync();
			setDeath(false);
			setReviveTime(0);
			LogServer.recordLog(player, ConstantsLog.battle, 40021, reviveCount, gold);
			LogServer.costEventLog(player.getPlayerId(), gold, ConstantsCost.siegeRevive);
			if (future != null) {
				future.cancel(true);
			}
			if (player.getDataInfo().getPet() != null) {
				player.getDataInfo().getPet().fullHpMp();
			}
			return true;
		}
	}

	@Override
	public int compareTo(SiegePlayer o) {
		if (o.getPlayerLevel() > getPlayerLevel()) {
			return 1;
		} else if (o.getPlayerLevel() < getPlayerLevel()) {
			return -1;
		} else {
			return 0;
		}
	}

	public SiegePlayerVO toSiegePlayerVO() {
		return new SiegePlayerVO(getPlayerId(), getPlayerLevel(), getPlayerName());
	}
}
